package fr.insalyon.sims.broadcast.tests;

import java.io.IOException;
import java.net.InetAddress;

import fr.insalyon.sims.broadcast.network.DataErrorEvent;
import fr.insalyon.sims.broadcast.network.DataErrorListener;
import fr.insalyon.sims.broadcast.network.DataEvent;
import fr.insalyon.sims.broadcast.network.DataListener;
import fr.insalyon.sims.broadcast.network.TCPStreamReceiver;
import fr.insalyon.sims.broadcast.network.TCPStreamTransmitter;

public class TCPStreamTest {
	// ------------------------------------------------------- CONSTANTS -- //
	// ------------------------------------------------------ ATTRIBUTES -- //
	// ---------------------------------------------------- CONSTRUCTORS -- //
	// --------------------------------------------------------- METHODS -- //
	
	public static void main(String[] args) throws IOException {
		int port = 22593;
		
		// Create TCP receiver stream
		TCPStreamReceiver receiver = new TCPStreamReceiver(port);
		receiver.addDataListener(new DataListener() {
			public void dataReceived(DataEvent event) {
				String data = new String(event.getData(), event.getOffset(), event.getLength());
				System.out.println("[receiver] data received: " + data);
			}
		});
		receiver.addDataErrorListener(new DataErrorListener() {
			public void streamError(DataErrorEvent event) {
				System.out.println("[receiver] an exception occured!");
				event.getException().printStackTrace();
			}
		});
		
		// Start the stream
		receiver.connect();
		receiver.start();
		System.out.println("[receiver] started");
		
		// Create TCP transmitter stream
		TCPStreamTransmitter transmitter = new TCPStreamTransmitter(InetAddress.getLocalHost(), port);
		transmitter.addDataErrorListener(new DataErrorListener() {
			public void streamError(DataErrorEvent event) {
				System.out.println("[transmitter] an exception occured!");
				event.getException().printStackTrace();
			}
		});
		
		// Start the stream
		transmitter.connect();
		transmitter.start();
		System.out.println("[transmitter] started");
		
		// Send an hello world every 2 seconds
		for (int i = 0; i < 10; i++) {
			String data = "hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !";
			
			transmitter.send(data.getBytes(), 0, data.length());
			
			System.out.println("[transmitter] hello world sent");

			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				// Ignored
			}
		}
		
		// Stop streams
		transmitter.stop();
		transmitter.disconnect();
		System.out.println("[transmitter] stopped");
		receiver.stop();
		receiver.disconnect();
		System.out.println("[receiver] stopped");
	}
	
	// ------------------------------------------------- PRIVATE METHODS -- //
	// --------------------------------------------------- INNER CLASSES -- //
}
